namespace Brjnk.GpxExplorer.Workspace
{
    using System;
    using System.ComponentModel;
    using Brjnk.Common;
    using Brjnk.CoordinateSystem2D;

    /// <summary>
    /// </summary>
    public class ViewManager : INotifyPropertyChanged
    {
        private ViewInfoBase view;

        public ViewManager(ViewInfoBase view)
        {
            this.view = view;
            view.ViewChanged += new EventHandler<EventArgs>(view_ViewChanged);
            CreateCommands();
        }

        private void view_ViewChanged(object sender, EventArgs e)
        {
            if (view.ZoomX > view.ZoomY)
            {
                Zoom = view.ZoomY;
                ZoomY = 1;
                ZoomX = view.ZoomX / view.ZoomY;
            }
            else
            {
                Zoom = view.ZoomX;
                ZoomX = 1;
                ZoomY = view.ZoomY / view.ZoomX;
            }
        }

        private double zoomX;

        public double ZoomX
        {
            get { return zoomX; }
            private set { zoomX = value; OnPropertyChanged("ZoomX"); }
        }

        private double zoomY;

        public double ZoomY
        {
            get { return zoomY; }
            private set { zoomY = value; OnPropertyChanged("ZoomY"); }
        }

        private double zoom;

        public double Zoom
        {
            get { return zoom; }
            private set { zoom = value; OnPropertyChanged("Zoom"); }
        }

        #region commands

        private void CreateCommands()
        {
            zoomInX = new GenericCommand(a => view.SetZoom(view.ZoomX * 1.25, view.ZoomY));
            zoomInY = new GenericCommand(a => view.SetZoom(view.ZoomX, view.ZoomY * 1.25));
            zoomOutX = new GenericCommand(a => view.SetZoom(view.ZoomX * 0.8, view.ZoomY));
            zoomOutY = new GenericCommand(a => view.SetZoom(view.ZoomX, view.ZoomY * 0.8));
            zoomIn = new GenericCommand(a => view.SetZoom(view.ZoomX * 1.25, view.ZoomY * 1.25));
            zoomOut = new GenericCommand(a => view.SetZoom(view.ZoomX * 0.8, view.ZoomY * 0.8));
        }

        private GenericCommand zoomInX;

        private GenericCommand zoomInY;

        private GenericCommand zoomOutX;

        private GenericCommand zoomOutY;

        private GenericCommand zoomIn;

        private GenericCommand zoomOut;

        public GenericCommand ZoomInXCommand { get { return zoomInX; } }

        public GenericCommand ZoomInYCommand { get { return zoomInY; } }

        public GenericCommand ZoomOutXCommand { get { return zoomOutX; } }

        public GenericCommand ZoomOutYCommand { get { return zoomOutY; } }

        public GenericCommand ZoomInCommand { get { return zoomIn; } }

        public GenericCommand ZoomOutCommand { get { return zoomOut; } }

        #endregion commands

        public event PropertyChangedEventHandler PropertyChanged;

        private void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null) PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
}